package com.biz.config;

import com.baomidou.mybatisplus.extension.plugins.*;
import com.baomidou.mybatisplus.extension.plugins.inner.*;
import com.biz.datascope.DataScopeHandle;
import com.biz.datascope.DataScopeInnerInterceptor;
import com.biz.datascope.DefaultDatascopeHandle;
import com.biz.interceptor.SqlBeautyInterceptor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @Description
 * @Author liuxi
 * @Date 2024/4/20
 **/
@Configuration
public class MybatisConfiguration {
    
    /**
     * 自动注入mybatis 优化器，
     * 配置可选择性装载  通过注解sql.beauty.show
     * 默认为true
     * 可以不匹配
     *
     * @return
     */
    @Bean
    @ConditionalOnProperty(name = {"sql.beauty.show"}, havingValue = "true", matchIfMissing = true)
    public SqlBeautyInterceptor sqlBeautyInterceptor() {
        return new SqlBeautyInterceptor();
    }

    /**
     * mjfz 默认数据权限处理器
     * @return MjfzDefaultDatascopeHandle
     */
    @Bean
    @ConditionalOnMissingBean
    public DataScopeHandle dataScopeHandle() {
        return new DefaultDatascopeHandle();
    }

    /**
     * mybatis plus 拦截器配置
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        // 自定义sql 过滤
        DataScopeInnerInterceptor dataScopeInnerInterceptor = new DataScopeInnerInterceptor();
        dataScopeInnerInterceptor.setDataScopeHandle(dataScopeHandle());
        mybatisPlusInterceptor.addInnerInterceptor(dataScopeInnerInterceptor);
        // 分页支持
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor());
        return mybatisPlusInterceptor;
    }

}
